Feature Group
bash internal / file and stream operations (43)
bash
This demonstrates redirecting both standard output and standard error to a single log file in Bash.
python hello.py > "output-and-error.log" 2>&1 # redirect both output and errors to output-and-error.log # &1 means file descriptor 1 (stdout), so 2>&1 redirects stderr (2) to the current # destination of stdout (1), which has been redirected to output-and-error.log.
bash internalfile and stream operationsstream redirection and pipingcombined redirection
bash
This demonstrates various methods of redirecting stdout
and stderr
in Bash, including appending, discarding, and combining streams.
python hello.py > output.txt # stdout to (file) python hello.py >> output.txt # stdout to (file), append python hello.py 2> error.log # stderr to (file) python hello.py 2>&1 # stderr to stdout python hello.py 2>/dev/null # stderr to (null) python hello.py >output.txt 2>&1 # stdout and stderr to (file), equivalent to &> python hello.py &>/dev/null # stdout and stderr to (null) echo "$0: warning: too many users" >&2 # print diagnostic message to stderr
bash internalfile and stream operationsstream redirection and pipingstdout redirection
bash
This demonstrates various uses of the cd
command in Bash for directory navigation, including moving to the home directory, going up one level, navigating to a specific directory, and returning to the last directory.
cd ~ # change to home directory cd # also goes to home directory cd .. # go up one directory # (^^say, from /home/username/Downloads to /home/username) cd /home/username/Documents # change to specified directory cd ~/Documents/.. # now in home directory (if ~/Documents exists) cd - # change to last directory # => /home/username/Documents
bash internalfile and stream operationsdirectory operationsdirectory navigation
bash
This demonstrates using a "here" document in Bash to create a Python script that interacts with stdout
, stderr
, and stdin
.
cat > hello.py << EOF #!/usr/bin/env python from __future__ import print_function import sys print("#stdout", file=sys.stdout) print("#stderr", file=sys.stderr) for line in sys.stdin: print(line, file=sys.stdout) EOF # Variables will be expanded if the first "EOF" is not quoted
bash internalfile and stream operationscontent operationshere-document (heredoc)
bash
This script redirects all output and errors from the python hello.py
command to /dev/null
, effectively suppressing any output or errors.
python hello.py > /dev/null 2>&1 # redirect all output and errors to the black hole, /dev/null, i.e., no output
bash internalfile and stream operationsstream redirection and pipingoutput suppression
bash
This demonstrates file and directory removal in Bash, including verbose and recursive deletion, and suggests using trash-cli
for safer file deletion.
rm -v output.out error.err output-and-error.log rm -r tempDir/ # recursively delete # You can install the `trash-cli` Python package to have `trash` # which puts files in the system trash and doesn't delete them directly # see https://pypi.org/project/trash-cli/ if you want to be careful
bash internalfile and stream operationsfile managementfile and directory deletion
bash
This code attempts to create a lock file using the noclobber
option to prevent overwriting. If the lock file already exists, it outputs an error message, demonstrating a simple file locking mechanism in Bash.
( set -o noclobber; echo > my.lock ) || echo 'Failed to create lock file'
bash internalfile and stream operationsfile operationsfile locking